#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <cstring>
#include <string>

using namespace std;

void Write(int wfd){
    string s = "i am father";
    write(wfd,s.c_str(),s.size());
}

void Read(int rfd){
    char buff[20];
    read(rfd, buff, sizeof(buff));
    printf("子进程%d收到:%s", getpid(),buff);
}

int main(void){
    int pipefd[2];
    int r = pipe(pipefd);
    if(r == -1){
        perror("Create pipe failed!");
        exit(1);
    }
    pid_t p = fork();
    if(p == 0){//子进程
        close(pipefd[1]);//关闭写端
        Read(pipefd[0]);
        close(pipefd[0]);
        exit(0);
    }
    //父进程
    close(pipefd[0]);//关闭读端
    Write(pipefd[1]);
    wait(NULL);
    close(pipefd[1]);
    return 0;
}